package com.example.recyclerviewdemo.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.recyclerviewdemo.R;
import com.example.recyclerviewdemo.listener.OnItemChildClickListener;
import com.example.recyclerviewdemo.listener.OnItemClickListener;

import java.util.List;

/**
 * @Author : dushaohui
 * @Time : on 2024/3/13 9:42
 * @Description : StringAdapter
 */
public class StringAdapter extends RecyclerView.Adapter<StringAdapter.ViewHodel> {

    private List<String> list;



    public StringAdapter(List<String> lists){
        this.list = lists;
    }

    @NonNull
    @Override
    public ViewHodel onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_text_rv, parent, false);
        ViewHodel viewHodel = new ViewHodel(view);
        view.setOnClickListener(v->{
            if(listener != null){
                listener.onItemClick(v,viewHodel.getAdapterPosition());
            }
        });
        //添加子控件点击事件
        view.findViewById(R.id.btn_test).setOnClickListener(v->{
            if(childClickListener != null){
                childClickListener.OnItemChildClick(v,viewHodel.getAdapterPosition());
            }
        });
        return viewHodel;
    }


    @Override
    public void onBindViewHolder(@NonNull ViewHodel holder, int position) {
        holder.tv.setText(list.get(position));
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHodel extends RecyclerView.ViewHolder {
        private TextView tv;
        public ViewHodel(@NonNull View itemView) {
            super(itemView);
            tv = itemView.findViewById(R.id.tv_text);
        }
    }


    private OnItemClickListener listener;
    private OnItemChildClickListener childClickListener;

    public void setOnItemClickListener(OnItemClickListener listener){
        this.listener = listener;
    }

    public void setOnItemChildClickListener(OnItemChildClickListener childClickListener){
        this.childClickListener = childClickListener;
    }


}
